package main

import (
	"github.com/micro/go-micro"
	"github.com/micro/cli"
	"global/core"
	"os"
	"fmt"
	"srv/analysis/lib/kline"
)

const (
	appName = "analysis"
	defaultConfigFile = "../../config/config.toml"
)

var configFile, workType, target, slug string
var ctx *core.Context

func main() {

	service := micro.NewService(
		micro.Name("go.micro.srv.analysis"),
		micro.Version("latest"),
		micro.Flags(
			cli.StringFlag{
				Name: 			"type",
				Usage: 			"Set analysis type",
			},
			cli.StringFlag{
				Name: 			"config",
				Usage: 			"Custom server config file",
			},
			cli.StringFlag{
				Name: 			"target",
				Usage: 			"Set coin or pair kline do you want to get",
			},
			cli.StringFlag{
				Name: 			"slug",
				Usage: 			"Set the name of exchange",
			},
		),
	)

	service.Init(
		micro.Action(func(context *cli.Context) {
			configFile = context.String("config")
			workType = context.String("type")
			target = context.String("target")
			slug = context.String("slug")
			if workType == "" {
				fmt.Println("Please set type option")
				os.Exit(1)
			}
			if configFile == "" {
				configFile = defaultConfigFile
			}
		}),
	)

	// 初始化数据库，日志，Redis等
	ctx = core.NewContext()
	ctx.Bootstrap(configFile, appName).Initialize()

	work()

	if err := service.Run(); err != nil {
		panic(err.Error())
	}
}

func work() {
	switch workType {
	case "ticker":
		// 分析币种的 ticker 数据
		analysis := NewAnalysis()
		analysis.Init()
		analysis.Run()
	case "kline":
		if target == "coin" {
			k := kline.NewKlineManager(ctx)
			k.Serv()
		} else {
			// get exchange
			m := kline.NewPairKlineManager(ctx, slug)
			m.Serv()
		}
	}
}
